package com.zador.secondHandHelp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;

import bean.Goods;
import bean.User;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends AppCompatActivity {
    User userS;
    ImageView image_head;
    EditText et_account;
    EditText et_passwd;
    Button btn_login;
    private Boolean bPwdSwitch = false;//图像切换判断
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Bmob.initialize(this,"9cf6ffe4f964af6c124dc3691b139fe5");
        image_head = findViewById(R.id.dl_tx);
        et_account = findViewById(R.id.dl_zh);
        et_passwd = findViewById(R.id.dl_mm);
        btn_login = findViewById(R.id.dl_dl);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findUser(et_account,et_passwd);
                if(userS != null){
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    Log.d("user", userS.getUserID());
                    intent.putExtra("user", userS);
                    startActivity(intent);
                }else{
                    Toast.makeText(LoginActivity.this,"用户或者密码输错，请重新输入",Toast.LENGTH_SHORT)
                            .show();
                }
            }
        });
        Button btn_register = findViewById(R.id.dl_zczh);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump_to_register();
            }
        });
    }

    public  void findUser(EditText et_account, EditText et_passwd) {
        BmobQuery<User> userAccount_BmobQuery = new BmobQuery<>();
        userAccount_BmobQuery.addWhereEqualTo("userID", et_account.getText().toString());//查询账号是否存在
        BmobQuery<User> userPwd_BmobQuery = new BmobQuery<>();
        userPwd_BmobQuery.addWhereEqualTo("password", et_passwd.getText().toString());//查询密码是否存在
        List<BmobQuery<User>> andqueries = new ArrayList<BmobQuery<User>>();//两者条件同时成立

        andqueries.add(userAccount_BmobQuery);
        andqueries.add(userPwd_BmobQuery);

        BmobQuery<User> query = new BmobQuery<>();
        query.and(andqueries);//查询符合整个and条件的人

        query.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if(e == null){
                    userS = list.get(0);
                    Message message = handler.obtainMessage();
                    message.what = 0;
                    message.obj = list;
                    handler.sendMessage(message);
                    Log.d("user", userS.getName());

                }else{
                    userS = null;
                    Log.d("null","null");
                }
            }
        });


    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    List<User> list = (List<User>) msg.obj;
                    userS = list.get(0);
                    break;
            }
        }
    };

    public void clickEyes(View view){
        final ImageView ivPwdSwitch = findViewById(R.id.iv_pwd_switch);
        et_passwd = findViewById(R.id.dl_mm);
        bPwdSwitch = !bPwdSwitch;
        if(bPwdSwitch){
            ivPwdSwitch.setImageResource(R.drawable.ic_visibility_black_24dp);
            et_passwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }else{
            ivPwdSwitch.setImageResource(R.drawable.ic_visibility_off_black_24dp);
            et_passwd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
            et_passwd.setTypeface(Typeface.DEFAULT);
        }

    }

    /**
     * 用做测试
     * @return
     */
    public void addPointer(View view) {
        Goods goods =new Goods();
        //task.setDeadline();
        goods.setPrice(15);
        goods.setDescription("te");
        goods.setName("hello");
        goods.setType("well");
        User userS =new User();
        userS.setObjectId("3kra111X");
        goods.setAccepter(userS);
        goods.setPublisher(userS);
        goods.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if(e!=null){
                    Toast.makeText(LoginActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(LoginActivity.this,"success",Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    public void jump_to_register(){
        Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(intent);
    }
}
